<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
import { enableStatusOptions } from '@/constants/business';

defineOptions({
  name: 'PoemOperateModal'
});

interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.SystemManage.Poem | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: $t('page.manage.poem.addPoem'),
    edit: $t('page.manage.poem.editPoem')
  };
  return titles[props.operateType];
});

type Model = Pick<
  Api.SystemManage.Poem,
  'id' | 'poemTitle' | 'author' | 'dynasty' | 'content' | 'description' | 'style' | 'tag' | 'status'
>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    id: 0,
    poemTitle: '',
    author: '',
    dynasty: '',
    content: '',
    description: '',
    style: '',
    tag: '',
    status: 1
  };
}

type RuleKey = Extract<keyof Model, 'poemTitle' | 'author' | 'content'>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  poemTitle: defaultRequiredRule,
  author: defaultRequiredRule,
  content: defaultRequiredRule
};

function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();
  // request
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
    restoreValidation();
  }
});
</script>

<template>
  <NDrawer v-model:show="visible" display-directive="show" :width="360">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem :label="$t('page.manage.poem.poemTitle')" path="poemTitle">
          <NInput v-model:value="model.poemTitle" :placeholder="$t('page.manage.poem.form.poemTitle')" />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.author')" path="author">
          <NInput v-model:value="model.author" :placeholder="$t('page.manage.poem.form.author')" />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.dynasty')" path="dynasty">
          <NInput v-model:value="model.dynasty" :placeholder="$t('page.manage.poem.form.dynasty')" />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.content')" path="content">
          <NInput
            v-model:value="model.content"
            type="textarea"
            :placeholder="$t('page.manage.poem.form.content')"
            :autosize="{ minRows: 3, maxRows: 6 }"
          />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.description')" path="description">
          <NInput
            v-model:value="model.description"
            type="textarea"
            :placeholder="$t('page.manage.poem.form.description')"
            :autosize="{ minRows: 2, maxRows: 4 }"
          />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.style')" path="style">
          <NInput v-model:value="model.style" :placeholder="$t('page.manage.poem.form.style')" />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.tag')" path="tag">
          <NInput v-model:value="model.tag" :placeholder="$t('page.manage.poem.form.tag')" />
        </NFormItem>
        <NFormItem :label="$t('page.manage.poem.status')" path="status">
          <NRadioGroup v-model:value="model.status">
            <NRadio v-for="item in enableStatusOptions" :key="item.value" :value="item.value" :label="$t(item.label)" />
          </NRadioGroup>
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>

<style scoped></style>
